package com.zxl.live.screen.call.helper;

import android.content.Context;
import android.text.TextUtils;

import com.zxl.live.screen.call.model.bean.ThemeBean;
import com.zxl.live.screen.call.ui.activity.NotificationAccessGuideAlertActivity;
import com.zxl.live.screen.screen.constant.ISharedPreferences;
import com.zxl.live.screen.screen.constant.ScreenSetting;
import com.zxl.live.tools.common.ApplicationHelper;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2017/12/11.
 */

public class ThemeHelper {

    public static String flashPath;

    public static String type;

    public static String yesPath;

    public static String noPath;

    public static boolean setCallFlashTheme(final Context context, final ThemeBean themeBean) {
        final JSONObject jsonObject = toJSONObject(themeBean);
        if (null != jsonObject) {
            if (!ScreenSetting.sCallFlashSwitch) {
                ScreenSetting.saveLockerSettings(ISharedPreferences.SETTINGS_CALL_FLASH_SWITCH, true);
            }
            ApplicationHelper.postThreadDelayed(new Runnable() {
                @Override
                public void run() {
                    ScreenSetting.saveLockerSettings(ISharedPreferences.SETTINGS_CALL_FLASH_INFO, jsonObject.toString());
                }
            }, 200);
            flashPath = themeBean.getDownloadPath();
            yesPath = themeBean.yes;
            noPath = themeBean.no;
            type = themeBean.type;
            NotificationAccessGuideAlertActivity.startGuideAlertPage(context);
        }
        return false;
    }

    public static void refreshCallFlashInfo() {
        ScreenSetting.init();
        init();
    }

    public static void init() {
        try {
            if (!TextUtils.isEmpty(ScreenSetting.sCallFlashInfo)) {
                JSONObject jsonObject = new JSONObject(ScreenSetting.sCallFlashInfo);
                flashPath = jsonObject.optString("src");
                type = jsonObject.optString("type", ThemeBean.TYPE_MP4);
                yesPath = jsonObject.optString("yes");
                noPath = jsonObject.optString("no");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static JSONObject toJSONObject(ThemeBean themeBean) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("src", themeBean.getDownloadPath());
            jsonObject.put("type", themeBean.type);
            jsonObject.put("yes", themeBean.yes);
            jsonObject.put("no", themeBean.no);
            return jsonObject;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

}
